<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="用户编号" prop="userId">
        <el-select
          v-model="queryParams.userId"
          style="width: 240px"
          filterable
        >
          <el-option
            v-for="dict in userOptions"
            :key="dict.id"
            :label="dict.phone + ' --- ' + dict.userName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日志类型" prop="logType">
        <el-select
          v-model="queryParams.logType"
          style="width: 240px"
          filterable
        >
          <el-option
            v-for="dict in logTypeOptions"
            :key="dict.val"
            :label="dict.lable"
            :value="dict.val"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="日志记录词典" prop="logName">
        <el-select
          v-model="queryParams.logName"
          style="width: 240px"
          filterable
        >
          <el-option

            label="购买饰品"
            value="购买饰品"
          />
          <el-option

            label="购买vip"
            value="购买vip"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="(充值提现有效)日志状态" prop="isStatus">
        <el-select
          v-model="queryParams.isStatus"
          style="width: 240px"
          filterable
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.val"
            :label="dict.lable"
            :value="dict.val"
          />
        </el-select>
      </el-form-item>


      <el-form-item label-width="220px" label="创建区间日期" prop="nowDate">
        <el-date-picker
          v-model="nowDates"
          type="datetimerange"
          range-separator="至"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:amount_log:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:amount_log:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:amount_log:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:amount_log:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="amount_logList"  sum-text="统计"    border
              show-summary :summary-method="getSummaries" @selection-change="handleSelectionChange">
      <el-table-column label="用户编号" align="center" prop="userId" :formatter="userFormatter" />
      <el-table-column label="日志记录词典" align="center" prop="logName" />
      <el-table-column label="日志金额" sortable align="center" prop="logCount" />
      <el-table-column label="提现到账金额(提现此字段有效)" sortable align="center" prop="logCount" :formatter="logCountFormatter" />

      <el-table-column label="日志类型" align="center" prop="logType" :formatter="logTypeFormatter" />
      <el-table-column label="(充值)(提现)状态" align="center" prop="isStatus" >
        <template slot-scope="scope">
          {{scope.row.isStatus == 1 ? '待审核' : scope.row.isStatus == 2 ? '失败' : '成功'  }}
        </template>
      </el-table-column>
      <el-table-column label="凭证地址" align="center" prop="imgUrl" >
        <template slot-scope="scope">
          <div v-if="scope.row.logType != 2">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.imgUrl"
              :preview-src-list="[scope.row.imgUrl]">
            </el-image>

          </div>
          <div v-else>
            <p>提现方式: {{ scope.row.logName }}</p>
            <p>支付宝账号: {{ scope.row.aliAccount }}</p>
            <p>支付宝姓名: {{ scope.row.aliUserName }}</p>
            <p>银行卡号: {{ scope.row.bankCar }}</p>
            <p>银行卡姓名: {{ scope.row.bankName }}</p>
          </div>

        </template>
      </el-table-column>
      <el-table-column label="创建时间" sortable align="center" prop="createTime" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:amount_log:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:amount_log:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[50, 200, 300, 400]"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改支出收益对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="日志词典" prop="logName">
          <el-input v-model="form.logName" placeholder="请输入日志词典" />
        </el-form-item>
        <el-form-item label="日志金额" prop="logCount">
          <el-input v-model="form.logCount" placeholder="请输入日志金额" />
        </el-form-item>
        <el-form-item label="凭证地址" prop="imgUrl">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="form.imgUrl"
              :preview-src-list="[form.imgUrl]">
            </el-image>
          </template>
        </el-form-item>

        <el-form-item label="日志类型" prop="logType">
          <el-select
            v-model="form.logType"
            style="width: 240px"
            filterable
          >
            <el-option
              v-for="dict in logTypeOptions"
              :key="dict.val"
              :label="dict.lable"
              :value="dict.val"
            />
          </el-select>
        </el-form-item>



        <el-form-item label="日志状态" v-if="form.logType == 1 || form.logType == 2 " prop="isStatus">
          <el-select
            v-model="form.isStatus"
            style="width: 240px"
            filterable
          >
            <el-option
              v-for="dict in statusOptions"
              :key="dict.val"
              :label="dict.lable"
              :value="dict.val"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAmount_log, getAmount_log, delAmount_log, addAmount_log, updateAmount_log, exportAmount_log } from "@/api/admin/amount_log";
import { listUser} from "@/api/admin/user";
import {  getRule_dates } from "@/api/admin/rule_dates";

export default {
  name: "Amount_log",
  data() {
    return {
      // 遮罩层
      loading: true,
      //1充值 2提现 3消费 4收入 5下级购买vip获得直推奖励

      logTypeOptions:[
        {
          lable:'充值',
          val:1
        },
        {
          lable:'提现',
          val:2
        },
        {
          lable:'消费',
          val:3
        },
        {
          lable:'收入',
          val:4
        },
        {
          lable:'直推奖励',
          val:5
        },
      ],
      //1待审核 2失败 3成功
      statusOptions:[
        {
          lable:'待审核',
          val:1
        },
        {
          lable:'失败',
          val:2
        },
        {
          lable:'成功',
          val:3
        },

      ],
      // 选中数组
      ids: [],
      userOptions:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支出收益表格数据
      amount_logList: [],
      nowDates:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      count:0.0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        userId: null,

        logName: null,

        logCount: null,

        logType: null,

        isStatus: null,

        imgUrl: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
    listUser({pageNum: 1,pageSize: 100000}).then(res =>{
      this.userOptions = res.rows;
    })
    getRule_dates('5').then(res =>{
      this.count =  res.data.ruleCount;
    });
  },
  methods: {
    /** 查询支出收益列表 */
    logTypeFormatter(row){
      var temp;
      this.logTypeOptions.forEach(item =>{
        if (item.val == row.logType){
          temp =  item.lable;
        }
      });
      return temp;
    },
    logCountFormatter(row){
      if (row.logType == 2){
        // 计算
         // temp =  Math.floor(parseInt(row.logCount) * this.count) / 100
        return  row.logCount - row.logCount * this.count / 100;

      }else{
        return row.logCount;
      }

    },
    userFormatter(row){
      var temp = "";
      this.userOptions.forEach(item =>{
        if (row.userId == item.id){
          temp = item.phone + ' --- ' + item.userName
        }
      })
      return temp;
    },

    getList() {
      this.loading = true;
      listAmount_log(this.queryParams).then(response => {
        this.amount_logList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        userId: null,

        logName: null,

        logCount: null,

        logType: null,

        createTime: null,

        isStatus: 0,

        imgUrl: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.nowDates.length >=2 ) {
        this.queryParams.creStartDate = this.nowDates[0]
        this.queryParams.creEndDate = this.nowDates[1]
      }

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.nowDates = []
      this.queryParams.creStartDate = null
      this.queryParams.creEndDate = null
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支出收益";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAmount_log(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改支出收益";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAmount_log(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAmount_log(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除支出收益编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAmount_log(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有支出收益数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAmount_log(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
      getSummaries(param) {
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '统计';
            return;
          }
          if (column.label != '日志金额'){
            return;
          }
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            sums[index] += ' 元';
          } else {
            sums[index] = 'N/A';
          }
        });

        return sums;
      },
  }
};
</script>
